package com.example.teaching_server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 教育主题实体类
 * 用于组织和管理教育内容主题
 *
 * @author Teaching Server
 * @since 1.0.0
 */
@Getter
@Setter
@ToString(exclude = {"parent", "subThemes", "contents"})
@EqualsAndHashCode(of = "id")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "education_themes", indexes = {
        @Index(name = "idx_theme_name", columnList = "name"),
        @Index(name = "idx_theme_parent", columnList = "parent_id"),
        @Index(name = "idx_theme_sort", columnList = "sort_order")
})
public class EducationTheme {

    /**
     * 主题ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 主题名称（英文标识）
     */
    @NotBlank(message = "主题名称不能为空")
    @Size(max = 100, message = "主题名称长度不能超过100个字符")
    @Column(name = "name", nullable = false, unique = true, length = 100)
    private String name;

    /**
     * 主题标签（中文显示）
     */
    @NotBlank(message = "主题标签不能为空")
    @Size(max = 100, message = "主题标签长度不能超过100个字符")
    @Column(name = "label", nullable = false, length = 100)
    private String label;

    /**
     * 主题描述
     */
    @Size(max = 1000, message = "主题描述长度不能超过1000个字符")
    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    @Column(name = "cover_image", length = 255)
    private String coverImage;

     /**
     * 是否为主主题
     */
    @NotNull(message = "是否为主主题不能为空")
    @Column(name = "is_main", nullable = false)
    @Builder.Default
    private Boolean isMain = false;

    /**
     * 父级主题
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    private EducationTheme parent;

    /**
     * 子主题列表
     */
    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @OrderBy("sortOrder ASC")
    @Builder.Default
    private List<EducationTheme> subThemes = new ArrayList<>();

    /**
     * 主题内容列表
     */
    @OneToMany(mappedBy = "theme", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @OrderBy("sortOrder ASC")
    @Builder.Default
    private List<ThemeContent> contents = new ArrayList<>();

    /**
     * 排序序号
     */
    @Column(name = "sort_order")
    @Builder.Default
    private Integer sortOrder = 0;

    /**
     * 创建人ID
     */
    @NotNull(message = "创建人ID不能为空")
    @Column(name = "created_by", nullable = false)
    @Builder.Default
    private Long createdBy = 1L;

    /**
     * 创建时间
     */
    @CreationTimestamp
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "created_at", nullable = false, updatable = false)
    @Builder.Default
    private LocalDateTime createdAt = LocalDateTime.now();

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "updated_at", nullable = false)
    @Builder.Default
    private LocalDateTime updatedAt = LocalDateTime.now();

    /**
     * 添加子主题
     */
    public void addSubTheme(EducationTheme subTheme) {
        subThemes.add(subTheme);
        subTheme.setParent(this);
    }

    /**
     * 移除子主题
     */
    public void removeSubTheme(EducationTheme subTheme) {
        subThemes.remove(subTheme);
        subTheme.setParent(null);
    }

    /**
     * 添加主题内容
     */
    public void addContent(ThemeContent content) {
        contents.add(content);
        content.setTheme(this);
    }

    /**
     * 移除主题内容
     */
    public void removeContent(ThemeContent content) {
        contents.remove(content);
        content.setTheme(null);
    }
}
